package com.power.web;

import com.github.pagehelper.PageInfo;
import com.power.model.TClue;
import com.power.result.R;
import com.power.service.ClueService;
import jakarta.annotation.Resource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
public class ClueController {
    @Resource
    private ClueService clueService;

    @PreAuthorize(value = "hasAuthority('clue:list')")
    @GetMapping("/api/clue")
    public R getClueListByPage(@RequestParam(value = "currentPage", required = false, defaultValue = "1") Integer currentPage,
                               @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize) {
        PageInfo<TClue> clueByPage = clueService.selectClueListByPage(currentPage, pageSize);
        return R.OK(clueByPage);
    }

    @PreAuthorize(value = "hasAuthority('clue:import')")
    @PostMapping(value = "/api/clue/importExcel")
    public R uploadExcelClue(MultipartFile file, @RequestHeader("token") String token) throws IOException {//file变量名要和前端的参数名相同，不然接收不到
        clueService.uploadExcelClue(file.getInputStream(), token);
        return R.OK();
    }

    @PreAuthorize(value = "hasAuthority('clue:view')")
    @GetMapping("/api/clue/{id}")
    public R getClueDetail(@PathVariable Integer id) {
        TClue tClue = clueService.selectClueDetailById(id);
        return R.OK(tClue);
    }

    @PreAuthorize(value = "hasAuthority('clue:delete')")
    @DeleteMapping("/api/clue/{id}")
    public R deleteClue(@PathVariable Integer id) {
//        TClue tClue = clueService.selectClueDetailById(id);
        return R.OK();
    }
}
